#include <57header.h>

/* Usage: ./06_myls pathname */
int main(int argc, char *argv[]){
    ARGS_CHECK(argc,2);
    DIR *dirp = opendir(argv[1]);
    ERROR_CHECK(dirp, NULL, "opendir");

    // 解决stat函数的path参数问题
    int ret = chdir(argv[1]);
    ERROR_CHECK(ret, -1, "chdir");

    struct dirent *pdirent;
    while((pdirent = readdir(dirp)) != NULL){
        struct stat statbuff;
        int ret2 = stat(pdirent->d_name,&statbuff);
        ERROR_CHECK(ret2, -1, "stat");
        
        // 打印文件的信息
        printf("%o\t%lu\t%u\t%u\t%lu\t%ld\t%s\n",
               statbuff.st_mode,
               statbuff.st_nlink,
               statbuff.st_uid,
               statbuff.st_gid,
               statbuff.st_size,
               statbuff.st_mtim.tv_sec,
               pdirent->d_name);
    }
    closedir(dirp);
    return 0;
}

